/**
 * 表格的基本结构方法
 */
import { mapGetters } from 'vuex'
import { count } from '../../assets/js/help.js'
export default {
  computed: {
    ...mapGetters({
      isHideSideBar: 'getIsHideSideBar',
      isFullScreenTable: 'getFullScreenTable',
      isMask: 'getIsMask'
    }),
    //表格的Id
    // 'tableId': function () {
    //   return this.thisRouter + 'Table';
    // }
  },

  created () {
    // this.thisRouter = this.$route.meta;
    if(this.tableId == '') {
      this.tableId = this.thisRouter + 'Table';
    }
  },

  mounted () {
    let _this = this;
    let resize_timer = null;

    this.resetTableFn();
    setTimeout(function(){
      this.resetTableFn();
    }.bind(this), 0);
    

    //resize 浏览器的时候需要重新绑定固定表头插件
    $(window).on('resize', function() {
      clearTimeout(resize_timer);
      resize_timer = setTimeout(function() {
        this.isShowSetting = false;
        this.resetTableFn();
        // this.resetFixedTableFn();
        // this.tHeight = $('#tableBox').height();
      }.bind(_this),300);
    })
  },

  methods: {
    //处理表格checkbox多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.$emit('handleMultiCheck', val);
    },

    handleSortChange(sortInfo) {
      let type = sortInfo.order == null ? '' : sortInfo.order;
      let prop = sortInfo.prop == null ? '' : sortInfo.prop;
      let meta = this.$route.meta;
      let info = JSON.parse(localStorage.getItem(meta));
      let sortObj = {
        orderBy: prop,
        orderTy: type
      }; 
      this.orderBy = prop;
      this.orderTy = type;
      if(!info) {info = {}; }
      if(!prop && !type) {
        delete info.sort;
      } else {
        info['sort'] = sortObj;
      }
      if(count(info) > 0){
        localStorage.setItem(meta, JSON.stringify(info));
      } else {
        localStorage.removeItem(meta);
      }
      type = type == '' ? '' : (type == 'ascending' ? 'ASC' : 'DESC');
      this.$emit('handleSortColumn', prop, type);
      this.$emit('searchFn');
    },

    //重置表格高度
    resetTableFn (isDelay) {
      if(isDelay) {
        setTimeout(function(){
          this.tHeight = this.getTable().height();
        }.bind(this), 300);
      } else {
        this.tHeight = this.getTable().height();
      }
    },

    //获取表格的jquery对象
    getTable () {
      return $('#'+this.tableId);
    },

    //切换全屏表格状态
    toggleFullScreenTable () {
      this.$store.commit('FULLSCREENTABLE_TOGGLE');
      this.$store.commit('SIDEBAR_TOGGLE',this.isFullScreenTable);
      this.resetTableFn(true);
    },

    //隐藏树的选择框
    toggleTreeBox () {
      // this.isShowTree = !this.isShowTree;
      this.$emit('toggleTreeBox', 'show');
    }
  }
  
}